package com.spark.lola.controller.purchaser;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.constant.SupOrderStatus;
import com.spark.lola.entity.pur.OrderInfo;
import com.spark.lola.entity.pur.OrderLocation;
import com.spark.lola.entity.pur.OrderProcess;
import com.spark.lola.feign.LogisticsFeignService;
import com.spark.lola.logistics.entity.LocationInfo;
import com.spark.lola.mapper.pur.OrderInfoMapper;
import com.spark.lola.request.CreateOrderRequest;
import com.spark.lola.request.UpdateOrderRequest;
import com.spark.lola.service.gds.IProductInfoService;
import com.spark.lola.service.pur.impl.OrderInfoServiceImpl;
import com.spark.lola.support.ai.params.LicensePlateRequest;
import com.spark.lola.support.ai.service.LicensePlateService;
import com.spark.lola.support.api.entity.EnterpriseTokenUser;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.mybatis.common.QueryCondition;
import com.spark.lola.support.mybatis.entity.PageParam;
import com.spark.lola.support.mybatis.entity.QueryParam;
import com.spark.lola.support.wms.request.OrderActionRequest;
import com.spark.lola.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Zhi-peng wang
 * @date: 2020/05/25 16:15
 **/
@RestController
@RequestMapping(value = "/api/pur/order", produces = "application/json;charset=utf-8")
@Api(value = "/api/pur/order", tags = "采购单接口")
public class PurOrderController {

    @Autowired
    private OrderInfoServiceImpl orderInfoService;

    @Autowired
    private LogisticsFeignService logisticsFeignService;

    @Autowired
    private IProductInfoService productInfoService;

    @Autowired
    private LicensePlateService licensePlateService;

    @Autowired
    private OrderInfoMapper orderInfoMapper;


    @ApiOperation(value = "分页查询采购单")
    @RequestMapping(value = "/findBy", method = RequestMethod.POST)
    public ResponseBean findBy(@RequestBody QueryParam<OrderInfo> queryBean, @SessionAttribute EnterpriseTokenUser<?> tokenUser) {
        QueryCondition queryCondition = new QueryCondition();
        if(queryBean.getQueryList().size()==0){
            // 过滤 已取消订单
            queryCondition.setKey("o.status");
            queryCondition.setOper("!=");
            queryCondition.setValue(SupOrderStatus.CANCELED);
            queryBean.getQueryList().add(queryCondition);
        }
        queryCondition = new QueryCondition();
        queryCondition.setKey("o.salesmanId");
        queryCondition.setOper("=");
        queryCondition.setValue(tokenUser.getShiroUser().getId());
        queryBean.getQueryList().add(queryCondition);
        queryCondition = new QueryCondition();
        queryCondition.setKey("o.status");
        queryCondition.setOper("!=");
        queryCondition.setValue(SupOrderStatus.DUSTBIN);
        queryBean.getQueryList().add(queryCondition);
        PageParam<OrderInfo> pageParam = queryBean.toPageParam();
        IPage<OrderInfo> page = orderInfoMapper.page(pageParam.getPage(), pageParam.getEntityWrapper());
        return ResponseBean.success(page);
    }

    @ApiOperation(value = "查询采购单详情")
    @ApiImplicitParam(name = "id", value = "采购单id", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/findInfo/{id}", method = RequestMethod.POST)
    public ResponseBean findInfo(@PathVariable Long id, @SessionAttribute EnterpriseTokenUser<?> tokenUser) {
        OrderVo vo = new OrderVo();
        OrderInfo orderInfo = orderInfoService.findFullById(id);
        List<OrderProcess> logs = orderInfoService.logs(id);
        vo.setOrderInfo(orderInfo);
        vo.setLogs(logs);
        return ResponseBean.success(vo);
    }

    @ApiOperation(value = "查询订单日志")
    @ApiImplicitParam(name = "id", paramType = "path", required = true)
    @RequestMapping(value = "/log/{id}", method = RequestMethod.POST)
    public ResponseBean log(@PathVariable Long id,
                            @SessionAttribute EnterpriseTokenUser<?> tokenUser) {
        return ResponseBean.success(orderInfoService.logs(id));
    }

    @ApiOperation(value = "查询纸厂列表")
    @RequestMapping(value = "/company", method = RequestMethod.POST)
    public ResponseBean company() {
        List<LocationInfo> lst = logisticsFeignService.getCompany();
        return ResponseBean.success(lst);
    }

    @ApiOperation(value = "查询货品列表")
    @RequestMapping(value = "/product", method = RequestMethod.POST)
    public ResponseBean product() {
        return ResponseBean.success(productInfoService.selectMap(1L, null));
    }

    @ApiOperation(value = "创建采购单")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean create(@RequestBody CreateOrderRequest request, @SessionAttribute EnterpriseTokenUser<?> tokenUser) throws Exception {
        OrderInfo form = new OrderInfo();
        BeanUtils.copyProperties(request, form);
        form.setSalesmanId(tokenUser.getShiroUser().getId());
        form.setCreatorId(tokenUser.getShiroUser().getId());
        form.setCreator(tokenUser.getShiroUser().getRealName());
        form.setUpdator(tokenUser.getShiroUser().getRealName());
        OrderInfo model = orderInfoService.add(form, tokenUser.getShiroUser());
        return ResponseBean.success(model);
    }

    @ApiOperation(value = "修改货品明细")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean update(@RequestBody UpdateOrderRequest request, @SessionAttribute EnterpriseTokenUser<?> tokenUser) throws Exception {
        OrderInfo form = new OrderInfo();
        BeanUtils.copyProperties(request, form);
        OrderInfo model = orderInfoService.update(form, tokenUser.getShiroUser());
        return ResponseBean.success(model);
    }

    @ApiOperation(value = "修改采购单地址")
    @RequestMapping(value = "/update-location", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean updateLocation(@RequestBody OrderLocation form, @SessionAttribute EnterpriseTokenUser<?> tokenUser) throws Exception {
        if (!orderInfoService.updateLoaction(form)) {
            return ResponseBean.error("保存失败!");
        }
        return ResponseBean.success(form);
    }

    @ApiOperation(value = "识别车牌号")
    @RequestMapping(value = "/check-license-plate", method = RequestMethod.POST)
    public ResponseBean checkLicensePlate(@RequestBody LicensePlateRequest request, @SessionAttribute EnterpriseTokenUser<?> tokenUser) throws Exception {
        return licensePlateService.discern(0, request.getBase64Image());
    }

    @ApiOperation(value = "采购单发货")
    @RequestMapping(value = "/checking", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean checking(@RequestBody OrderActionRequest request, @SessionAttribute EnterpriseTokenUser<?> tokenUser) throws Exception {
        orderInfoService.checking(request, tokenUser.getShiroUser());
        return ResponseBean.success();
    }

    @ApiOperation(value = "采购单取消")
    @RequestMapping(value = "/canceling", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean canceling(@RequestBody OrderActionRequest request, @SessionAttribute EnterpriseTokenUser<?> tokenUser) throws Exception {
        orderInfoService.canceling(request, tokenUser.getShiroUser());
        return ResponseBean.success();
    }

    @ApiOperation(value = "采购单删除")
    @ApiImplicitParam(name = "id", value = "采购单id", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST)
    public ResponseBean del(@PathVariable long id) {
        orderInfoService.removeById(id);
        return ResponseBean.success();
    }

    @ApiOperation(value = "加入垃圾箱")
    @RequestMapping(value = "/recycle/{id}", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean recycle(@PathVariable Long id,
                                @SessionAttribute EnterpriseTokenUser<?> tokenUser) throws Exception {
        orderInfoService.recycle(id,tokenUser.getShiroUser());
        return ResponseBean.success();
    }
}
